# TextMenu - Implements basic menu that contains text labels as menuitems
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import clutter
from frontend.gui.widgets.selector import Selector
from frontend.gui.widgets.grid_menu import GridMenu

class TextMenu(GridMenu):
    """
    TextMenu widget
    
    This widget inherits genral GridMenu widget and implements simple textmenu.
    """
    
    def __init__(self, theme, animate):
        """
        Initialize widget
        
        @param theme: Theme object
        @param animate: Animate this menu widget (boolean)
        """
        GridMenu.__init__(self)
        
        self.animate = animate              # Animate this widget behaviour
        self.set_item_size(570, 60)         # Default size for textmenu item
        
        # Set Cursor
        self.set_cursor_placement(self.BELOW)
        c = Selector(Selector.LARGE, theme)
        c.animate(animate)
        self.set_cursor(c)
        
    def _update_selected_item(self, new_location, old_location=None):
        """
        Overridden method! See GridMenu API reference.
        @param new_location: Logical location of current menuitem
        @param old_location: Logical location of last menuitem
        """
        if old_location is not None:
            self.items[old_location].set_active(False)
        self.items[new_location].set_active(True)
        
    def _animate_items_on_state_change(self):
        """
        This method is called when widget is set active or inactive. This method
        allows widget to react this action.
        """
        try:
            if self.active:
                self.cursor.show()
                self.items[self.logical_position].set_active(True)
                self.set_opacity(255)
            else:
                self.cursor.hide()
                self.items[self.logical_position].set_active(False)
                self.set_opacity(128)
        except:
            pass # No items in menu